const util = require('../../utils/util.js')
const rpn = require('../../utils/rpn.js')
Page({
  data:{
    msg:'0',
    id1:'AC',
    id2:'+/-',
    id3:'%',
    id4:'÷',
    id5:'7',
    id6:'8',
    id7:'9',
    id8:'*',
    id9:'4',
    id10:'5',
    id11:'6',
    id12:'-',
    id13:'1',
    id14:'2',
    id15:'3',
    id16:'+',
    id17:'0',
    id18:'.',
    id19:'=',
  },
  getContent:function(e){
    var id = e.target.id;
    var initData = this.data.msg;
    if (id == this.data.id1){
      this.setData({
        msg: 0
      });
    }else if( id == this.data.id2){//取反
      if (initData == "0"){
          return
      }
      var firstWord = initData.charAt(0);
      if (firstWord == "-"){
        initData = initData.substr(1);
      }else{
        initData = "-" + initData;
      }
      this.setData({
        msg: initData
      });
    }else if(id == this.data.id3){//百分比运算
      if (initData == 0){
        return;
      }else{
        initData = initData/100;
        this.setData({
          msg: initData
        });
      }
    }else if( id == this.data.id19){
      //如果只有一个数字且是0，则直接返回
      if( initData == 0){
        return;
      }
      //判断最后一位是否为是有效数字
      var lastWord = initData.charAt(initData.length - 1);
      if (isNaN(lastWord)) {
        return;
      }
      var calData = rpn.calCommonExp(initData);
      console.log(calData);
      this.setData({
        msg: calData
      });
    }
    else{
      var total;
      if (initData == 0){
        total = id
      }
      else{
        total = initData + id;
      }
      this.setData({
        msg: total
      });
    }
  }
})